package 题目集.图.最短路;

import java.util.*;

/**
 * https://www.acwing.com/problem/content/4278/
 * 给你一个无向图
 * 给你一些序列，让你判断这些序列是否可以通过Dijkstra跑出来
 */
public class Dijkstra序列 {
    static List<Edge>[] list;
    static int n, m;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        list = new List[n + 1];
        for (int i = 1; i < list.length; i++) {
            list[i] = new ArrayList<>();
        }
        for (int i = 0; i < m; i++) {
            int from = sc.nextInt();
            int to = sc.nextInt();
            int w = sc.nextInt();
            Edge fromTo = new Edge(from, to, w);
            Edge toFrom = new Edge(to, from, w);
            list[from].add(fromTo);
            list[to].add(toFrom);
        }
        int t = sc.nextInt();
        for (int i = 0; i < t; i++) {
            int[] path = new int[n + 1];
            for (int j = 1; j < path.length; j++) {
                path[j] = sc.nextInt();
            }
            int[] dis = dijkstra(path[1]);
            int pre = -1;
            boolean flag = true;
            for (int j = 1; j <= n; j++) {
                int cur = dis[path[j]];
                if (cur < pre) {
                    flag = false;
                    break;
                }
                pre = cur;
            }
            if (flag) {
                System.out.println("Yes");
            } else {
                System.out.println("No");
            }
        }
    }

    public static int[] dijkstra(int s) {
        int[] dis = new int[n + 1];
        boolean[] vis = new boolean[n + 1];
        Arrays.fill(dis, Integer.MAX_VALUE);
        PriorityQueue<int[]> queue = new PriorityQueue<>((a, b) -> a[1] - b[1]);    //0位置点编号，1位置距离
        queue.add(new int[]{s, 0});
        dis[s] = 0;
        int cnt = 0;  //已经完成的点
        while (!queue.isEmpty()&&cnt<n) {
            int[] poll = queue.poll();
            int id = poll[0];
            int d = poll[1];
            if (vis[id]) continue;  //有可能存在两个点存在多条边的情况，如果这样某个点可能会被反复弹出。所以加上vis
            vis[id]=true;
            cnt++;
            for (Edge edge : list[id]) {
                int newDis = edge.w + d;
                if (newDis < dis[edge.to]) {
                    dis[edge.to] = newDis;
                    queue.add(new int[]{edge.to, newDis});
                }
            }
        }
        return dis;
    }

    static class Edge {
        int from, to;
        int w;

        public Edge(int from, int to, int w) {
            this.from = from;
            this.to = to;
            this.w = w;
        }
    }
}
